﻿using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Ecobee.Net
{
	/// <summary>
	/// This request retrieves a list of thermostat configuration and state revisions. This request is a 
	/// light-weight polling method which will only return the revision numbers for the significant 
	/// portions of the thermostat data. It is the responsibility of the caller to store these revisions 
	/// for future determination whether changes occurred at the next poll interval.
	/// </summary>
	public class ThermostatSummary : ApiObject
	{
		private Selection _selection = null;
		private SummaryRevisionList _summaryRevisionList = null;
		private ThermostatStatusList _thermostatStatusList = new ThermostatStatusList();

		/// <summary>
		/// Creates an instance of Ecobee.Net.ThermostatSummary with the specified Ecobee.Net.Api
		/// instance.
		/// </summary>
		/// <param name="api">The active instance of the Ecobee.Net.Api object used to communicate
		/// to the ecobee API.</param>
		public ThermostatSummary(Api api)
			: base(api)
		{
			this.SummaryRevisionList = new SummaryRevisionList(this.Api);
			this.ThermostatStatusList = new ThermostatStatusList();
		}

		/// <summary>
		/// Specifies the thermostats to perform the function(s) against.
		/// </summary>
		[JsonProperty(PropertyName = "selection", Order = 1)]
		public Selection Selection
		{
			get
			{
				return _selection;
			}
			set
			{
				_selection = value;
			}
		}

		/// <summary>
		/// The list of revision values.
		/// </summary>
		[JsonProperty(PropertyName = "revisionList", DefaultValueHandling = DefaultValueHandling.Ignore, Order = 2)]
		public SummaryRevisionList SummaryRevisionList
		{
			get
			{
				return _summaryRevisionList;
			}
			set
			{
				_summaryRevisionList = value;
			}
		}

		/// <summary>
		/// The list of status values. 
		/// </summary>
		[JsonProperty(PropertyName = "statusList", DefaultValueHandling = DefaultValueHandling.Ignore, Order = 3)]
		public ThermostatStatusList ThermostatStatusList
		{
			get
			{
				return _thermostatStatusList;
			}
			set
			{
				_thermostatStatusList = value;
			}
		}

		/// <summary>
		/// This request retrieves a list of thermostat configuration and state revisions. This request is a light-weight 
		/// polling method which will only return the revision numbers for the significant portions of the thermostat 
		/// data. It is the responsibility of the caller to store these revisions for future determination whether 
		/// changes occurred at the next poll interval.
		/// The intent is to permit the caller to determine whether a thermostat has changed since the last poll. 
		/// Retrieval of a whole thermostat including runtime data is expensive and impractical for large amounts of 
		/// thermostat such as a management set hierarchy, especially if nothing has changed. By storing the retrieved 
		/// revisions, the caller may determine whether to get a thermostat and which sections of the thermostat should 
		/// be retrieved.
		/// Revisions are UTC date/time stamps in the format: YYMMDDHHMMSS. However, due to possible time drift 
		/// between the API consumer, the server and thermostat, it is recommended that they are treated as a string, 
		/// rather than as a date/time stamp. The recommended method to test for revision changes is to simply do a 
		/// string comparison on the previous and current revision. If the strings match, nothing changed. Otherwise 
		/// request the thermostat including the relevant information which changed.
		/// </summary>
		public async Task<bool> Request(Selection selection = null)
		{
			bool returnValue = false;

			if (selection == null)
			{
				this.Selection = new Selection()
				{
					SelectionType = SelectionType.Registered,
					SelectionMatch = null
				};
			}
			else
			{
				this.Selection = selection;
			}

			// ***
			// *** Call the web service
			// ***
			ApiResult response = await this.Api.Http.Get(this.Api.KnownUri.RequestThermostatSummary(this));

			if (response.Successful)
			{
				if (response.DataObject.revisionList != null)
				{
					// ***
					// *** Parse the response
					// ***
					this.SummaryRevisionList.Load(response.DataObject.revisionList);
				}

				if (response.DataObject.statusList != null)
				{
					// ***
					// *** Parse the response
					// ***
					this.ThermostatStatusList.Load(response.DataObject.statusList);
				}

				returnValue = true;
			}
			else
			{
				response.ThrowException();
			}

			return returnValue;
		}

	}
}
